/*
 * Filename:    AuditDispatchController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-11-10
 */
package com.paic.mhis.hcpms.audit.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.util.HttpProcessUtil;
import com.paic.mhis.core.auth.biz.service.Authority;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.audit.biz.service.AuditProblemsService;
import com.paic.mhis.hcpms.audit.biz.service.AuditService;
import com.paic.mhis.hcpms.audit.biz.service.IAuditDispatchService;
import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.audit.dto.AuditDispatchRequestDTO;
import com.paic.mhis.hcpms.audit.dto.AuditDispatchResponseDTO;
import com.paic.mhis.hcpms.audit.dto.AuditDispatchUserRequestDTO;
import com.paic.mhis.hcpms.audit.dto.AuditProblemParameterRequestDTO;
import com.paic.mhis.hcpms.audit.dto.AuditUserInfoDTO;
import com.paic.mock.util.StringUtils;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

/**
 * 该控制器主要是对审核岗分配任务
 * 
 * @author EX-WANGKUI001
 * 
 */
@Controller
@RequestMapping("/auditDispatch/")
public class AuditDispatchController {

	@BizServicesProxy(beanName = "auditDispatchServiceImpl")
	private IAuditDispatchService auditDispatchService;

	@BizServicesProxy(beanName = "auditProblemsService")
	private AuditProblemsService auditProblemsService;

	@BizServicesProxy
	private AuditService auditServiceImpl;

	/**
	 * 稽核经办刚
	 */
	public static final String role_name = "AUDIT_AGENT_ROLE";

	/**
	 * 加载稽核经办刚的角色用户信息
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadUserInfoByRoleName.do")
	@ResponseBody
	public Map<String, Object> loadUserInfoByRoleName(
			AuditProblemParameterRequestDTO vo, HttpServletRequest request) {

		List<AuditUserInfoDTO> list = this.auditDispatchService
				.queryAuditUserInfoByRoleName(role_name);

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("data", list);

		return map;

	}

	@RequestMapping("loadProblemInfo.do")
	@ResponseBody
	public Map<String, Object> loadProblemInfo(HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			int page = Integer.parseInt(request.getParameter("page"));
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));

			String problemCode = request.getParameter("problemCode");
			String hearDateStart = request.getParameter("hearDateStart");
			String hearDateEnd = request.getParameter("hearDateEnd");
			String problemStatus = request.getParameter("problemStatus");

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("problemCode", problemCode);
			map.put("hearDateStart", hearDateStart);
			map.put("hearDateEnd", hearDateEnd);
			map.put("problemStatus", problemStatus);
			getRequestMap(request, map);
			result = this.auditProblemsService
					.queryAuditProblemBeanByParameterBean(page, pageSize, map,
							result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 分配任务
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "dispatchUser.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> auditConfirmProcess(HttpServletRequest request)
			throws Exception {
		Map<String, Object> result = null;
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			AuditDispatchUserRequestDTO vo = JSONObject.toJavaObject(
					transferJson, AuditDispatchUserRequestDTO.class);

			String username = user.getUsername();
			vo.setOpertionUsername(username);

			result = this.auditDispatchService.dispatchTaskByUserCode(vo,
					transferJson, user);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 根据事件编码加载对应的医疗机构和参保人信息，医护人员
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadMedicalInfoList.do")
	@ResponseBody
	public AuditDispatchResponseDTO loadMedicalInfoList(@ModelAttribute
	AuditDispatchRequestDTO vo, HttpServletRequest request) {

		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");

		AuditDispatchResponseDTO medicalInfo = this.auditDispatchService
				.queryMedicalInfo(vo);
		AuditDispatchResponseDTO staffInfo = this.auditDispatchService
				.queryStaffInfo(vo, user);
		AuditDispatchResponseDTO insurnaceInfo = this.auditDispatchService
				.queryInsuranceInfo(vo, user);

		AuditDispatchResponseDTO returnBean = new AuditDispatchResponseDTO();

		returnBean.setInsuranceBean(insurnaceInfo.getInsuranceBean());
		returnBean.setStaffBean(staffInfo.getStaffBean());
		returnBean.setMedicalBean(medicalInfo.getMedicalBean());
		return returnBean;
	}

	/**
	 * 根据事件编码加载对应的事件实体
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadProblemData.do")
	@ResponseBody
	public AuditDispatchResponseDTO loadProblemData(@ModelAttribute
	AuditDispatchRequestDTO vo, HttpServletRequest request) {
		AuditDispatchResponseDTO bean = this.auditDispatchService
				.queryAuditProblemsDTO(vo);
		return bean;

	}

	/**
	 * 查询待办任务 注意：前述方法loadProblemInfo，查询的是流程之外， 或者无法通过流程待办任务获取的列表;
	 * 本方法是事件任务的待办任务列表查询方法。
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("findProblemTaskList.do")
	@ResponseBody
	public Map<String, Object> loadProblemTaskList(HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			int page = Integer.parseInt(request.getParameter("page"));
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));

			String problemCode = request.getParameter("problemCode");
			String hearDateStart = request.getParameter("hearDateStart");
			String hearDateEnd = request.getParameter("hearDateEnd");
			String problemStatus = request.getParameter("problemStatus");

			Map<String, Object> map = new HashMap<String, Object>();
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			map.put("assigner", user.getUsername().toUpperCase());
			List<Authority> authorList = user.getAuthorities();
			StringBuffer sb = new StringBuffer();
			String role = "";
			for (Authority authority : authorList) {
				sb = sb.append("'" + authority.getAuthority() + "',");
			}
			if (sb != null) {
				role = sb.substring(0, sb.length() - 1);
			}
			map.put("role", role);// 替换掉AUDITING_ROLE，改为查询用户的角色

			map.put("problemCode", problemCode);
			map.put("hearDateStart", hearDateStart);
			map.put("hearDateEnd", hearDateEnd);
			map.put("problemStatus", problemStatus);
			getRequestMap(request, map);
			result = auditServiceImpl.findProblemPage(page, pageSize, map,
					result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 查询待办任务 注意：前述方法loadProblemInfo，查询的是流程之外， 或者无法通过流程待办任务获取的列表;
	 * 本方法是事件任务的待办任务列表查询方法。
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("findProblemAuditTaskList.do")
	@ResponseBody
	public Map<String, Object> findProblemAuditTaskList(
			HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			int page = Integer.parseInt(request.getParameter("page"));
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));

			String problemCode = request.getParameter("problemCode");
			String hearDateStart = request.getParameter("hearDateStart");
			String hearDateEnd = request.getParameter("hearDateEnd");
			String problemStatus = request.getParameter("problemStatus");
			String[] problemStatuss = problemStatus.split(",");
			Map<Object, Object> map = new HashMap<Object, Object>();
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			map.put("assigner", user.getUsername().toUpperCase());

			map.put("problemCode", problemCode);
			map.put("hearDateStart", hearDateStart);
			map.put("hearDateEnd", hearDateEnd);
			map.put("problemStatus", problemStatuss);

			String orgName = request.getParameter("orgName");
			String sourceType = request.getParameter("sourceType");
			String doctorName = request.getParameter("doctorName");
			String jobLicense = request.getParameter("jobLicense");
			String start = request.getParameter("start");
			String end = request.getParameter("end");

			if (StringUtils.hasText(orgName)) {
				map.put("orgName", "%" + orgName + "%");
			}

			if (StringUtils.hasText(sourceType)) {
				map.put("sourceType", sourceType);
			}
			if (StringUtils.hasText(doctorName)) {
				map.put("doctorName", "%" + doctorName + "%");
			}
			if (StringUtils.hasText(jobLicense)) {
				map.put("jobLicense", jobLicense);
			}
			if (StringUtils.hasText(start)) {
				map.put("start", start);
			}
			if (StringUtils.hasText(end)) {
				map.put("end", end);
			}
			// map.put("sourceType", sourceType);
			// map.put("doctorName", "%"+doctorName+"%");
			// map.put("jobLicense", jobLicense);
			// map.put("start", start);
			// map.put("end", end);
			// getRequestMap(request, map);
			result = auditServiceImpl.findProblemAuditPage(page, pageSize, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 查询待办任务 注意：前述方法loadProblemInfo，查询的是流程之外， 或者无法通过流程待办任务获取的列表;
	 * 本方法是事件任务的待办任务列表查询方法。
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("findCaseTaskList.do")
	@ResponseBody
	public Map<String, Object> loadCaseTaskList(HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			int page = Integer.parseInt(request.getParameter("page"));
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));

			String caseCode = request.getParameter("caseCode");
			String beginDate = request.getParameter("beginDate");
			String endDate = request.getParameter("endDate");
			String caseStatus = request.getParameter("caseStatus");

			Map<String, Object> map = new HashMap<String, Object>();
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			map.put("assigner", user.getUsername().toUpperCase());
			List<Authority> authorList = user.getAuthorities();
			StringBuffer sb = new StringBuffer();
			String role = "";
			for (Authority authority : authorList) {
				sb = sb.append("'" + authority.getAuthority() + "',");
			}
			if (sb != null) {
				role = sb.substring(0, sb.length() - 1);
			}
			map.put("role", role);// 替换掉AUDITING_ROLE，改为查询用户的角色

			map.put("caseCode", caseCode);
			map.put("beginDate", beginDate);
			map.put("endDate", endDate);
			map.put("caseStatus", caseStatus);
			getRequestMap(request, map);
			result = auditServiceImpl.findCasePage(page, pageSize, map, result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 查询待办任务 注意：前述方法loadProblemInfo，查询的是流程之外， 或者无法通过流程待办任务获取的列表;
	 * 本方法是事件任务的待办任务列表查询方法。
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("findCaseAuditTaskList.do")
	@ResponseBody
	public Map<String, Object> findCaseAuditTaskList(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			int page = Integer.parseInt(request.getParameter("page"));
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));

			String caseCode = request.getParameter("caseCode");
			String beginDate = request.getParameter("beginDate");
			String endDate = request.getParameter("endDate");
			String caseStatus = request.getParameter("caseStatus");

			Map<String, Object> map = new HashMap<String, Object>();
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			map.put("assigner", user.getUsername().toUpperCase());

			map.put("caseCode", caseCode);
			map.put("beginDate", beginDate);
			map.put("endDate", endDate);
			map.put("caseStatus", caseStatus);
			getRequestMap(request, map);
			result = auditServiceImpl.findCaseAuditPage(page, pageSize, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 事件管理，查询ProblemStaus=1的事件
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("findProblemListByStatus.do")
	@ResponseBody
	public Map<String, Object> findProblemListByStatus(
			HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			int page = Integer.parseInt(request.getParameter("page"));
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));

			String problemCode = request.getParameter("problemCode");
			String hearDateStart = request.getParameter("hearDateStart");
			String hearDateEnd = request.getParameter("hearDateEnd");
			String problemStatus = request.getParameter("problemStatus");

			Map<String, Object> map = new HashMap<String, Object>();
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			map.put("assigner", user.getUsername().toUpperCase());

			map.put("problemCode", problemCode);
			map.put("hearDateStart", hearDateStart);
			map.put("hearDateEnd", hearDateEnd);
			map.put("problemStatus", problemStatus);
			getRequestMap(request, map);
			result = auditServiceImpl.findProblemPageByStatus(page, pageSize,
					map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public void getRequestMap(HttpServletRequest request,
			Map<String, Object> map) {
		String orgName = request.getParameter("orgName");
		String sourceType = request.getParameter("sourceType");
		String doctorName = request.getParameter("doctorName");
		String jobLicense = request.getParameter("jobLicense");
		String start = request.getParameter("start");
		String end = request.getParameter("end");

		map.put("orgName", orgName);
		map.put("sourceType", sourceType);
		map.put("doctorName", doctorName);
		map.put("jobLicense", jobLicense);
		map.put("start", start);
		map.put("end", end);
	}

	/**
	 * 事件管理，批量提交 单独分配一个事件分配调查人员的方法：auditConfirmProcess
	 */
	@RequestMapping("problemManagementBatchSubmit.do")
	@ResponseBody
	public Map<String, Object> problemManagementBatchSubmit(
			HttpServletRequest request) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		String[] problemCodeList = request.getParameterValues("problemCodes");
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		try {
			result = auditDispatchService.problemManagementBatchSubmit(
					problemCodeList, user);
		} catch (Exception e) {
			result.put("state", AuditConstants.FLAG_FALSE);
			result.put("msg", AuditConstants.OPERATE_FAILED);
			e.printStackTrace();
		}
		return result;
	}

}
